﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BuXiaChu.OrderDishes.BLL.StaticCache;
using BuXiaChu.OrderDishes.BLL.Constants;
using System.Web;
using System.IO;
using BuXiaChu.OrderDishes.BLL.Utility;

namespace BuXiaChu.OrderDishes.BLL
{
    public class ImageShowManage
    {
        #region
        private static string RarIDEncryptKey = "bxcimg12";
        private static string DishesIDEncryptKey = "bxcimg13";
        /// <summary>
        /// type:0 商店 1菜品
        /// </summary>
        /// <returns></returns>
        public static string GetEncryptID(string imageID, int type)
        {
            if (type == 0)
            {
                return EncryptionUtility.EncryptString(imageID, RarIDEncryptKey);
            }
            else
            {
                return EncryptionUtility.EncryptString(imageID, DishesIDEncryptKey);
            }
        }

        /// <summary>
        /// type:0 商店 1菜品
        /// </summary>
        public static string GetDecryptID(string encryptImageID, int type)
        {
            if (type == 0)
            {
                return EncryptionUtility.DecryptString(encryptImageID, RarIDEncryptKey);
            }
            else
            {
                return EncryptionUtility.DecryptString(encryptImageID, DishesIDEncryptKey);
            }
        }
        #endregion

        private static IList<string> ImageExtendName
        {
            get;
            set;
        }

        static ImageShowManage()
        {
            #region
            ImageExtendName = new List<string>();
            ImageExtendName.Add(".jpg");
            ImageExtendName.Add(".gif");
            ImageExtendName.Add(".jpeg");
            ImageExtendName.Add(".png");

            StaticCacheImage.Instance.CacheSubDirs = CacheConstants.CacheSubDirs;
            Dishes = new DishesImageCache();
            #endregion
        }

        public static IImageShowCache Restaurant
        {
            get;
            private set;
        }

        public static IImageShowCache Dishes
        {
            get;
            private set;
        }


        public static byte[] GetDishesImage(decimal imageID)
        {
            var image = RestaurantManager.GetDishesById(imageID).Image;
            if (image == null)
            {
                return new byte[0];
            }
            return image;
        }

        public static bool ValidateImageExtendName(string imagePath)
        {
            if (!string.IsNullOrEmpty(imagePath))
            {
                return ImageExtendName.Contains(Path.GetExtension(imagePath).ToLower());
            }
            return false;
        }

        public static byte[] GetImageContent(HttpPostedFileBase httpPostedFile)
        {
            if (httpPostedFile != null)
            {
                byte[] imgContent = new byte[httpPostedFile.ContentLength];
                httpPostedFile.InputStream.Read(imgContent, 0, imgContent.Length);

                return imgContent;
            }
            return new byte[0];
        }
    }

    public interface IImageShowCache
    {
        void SetImageToCache(string imageID);
        void SetImageToCache(string imageID, byte[] imageContent);
        byte[] GetImageFromCache(string imageID);
    }

    public class DishesImageCache : IImageShowCache
    {
        #region
        public void SetImageToCache(string imageID)
        {
            byte[] content = ImageShowManage.GetDishesImage(decimal.Parse(imageID));
            SetImageToCache(imageID, content);
        }

        public void SetImageToCache(string imageID, byte[] imageContent)
        {
            StaticCacheImage.Instance.CachePath = HttpContext.Current.Server.MapPath(CacheConstants.DishesImageDirs);
            if (imageContent != null && imageContent.Length > 0)
            {
                StaticCacheImage.Instance.Set(imageID, imageContent);
            }
        }

        public byte[] GetImageFromCache(string imageID)
        {
            StaticCacheImage.Instance.CachePath = HttpContext.Current.Server.MapPath(CacheConstants.DishesImageDirs);
            byte[] content;
            if (StaticCacheImage.Instance.TryGet(imageID, out content))
            {
                return content;
            }
            return new byte[0];
        }
        #endregion
    }
}
